package cn.lblbc.appassit.model

import android.graphics.drawable.Drawable
import android.os.Parcel
import android.os.Parcelable

data class AppInfo(
    val appName: String,
    val packageName: String,
    val versionName: String,
    val versionCode: Long,
    val icon: Drawable?,
    val apkPath: String,
    val installTime: Long,
    val updateTime: Long,
    val isSystemApp: Boolean,
    val targetSdkVersion: Int,
    val minSdkVersion: Int,
    val apkSize: Long,
    val supports32Bit: Boolean,
    val supports64Bit: Boolean,
    val signatureInfo: String? = null
) : Parcelable {
    constructor(parcel: Parcel) : this(
        parcel.readString() ?: "",
        parcel.readString() ?: "",
        parcel.readString() ?: "",
        parcel.readLong(),
        null, // Drawable cannot be parceled directly
        parcel.readString() ?: "",
        parcel.readLong(),
        parcel.readLong(),
        parcel.readByte() != 0.toByte(),
        parcel.readInt(),
        parcel.readInt(),
        parcel.readLong(),
        parcel.readByte() != 0.toByte(),
        parcel.readByte() != 0.toByte(),
        parcel.readString()
    )

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(appName)
        parcel.writeString(packageName)
        parcel.writeString(versionName)
        parcel.writeLong(versionCode)
        // Drawable cannot be parceled directly
        parcel.writeString(apkPath)
        parcel.writeLong(installTime)
        parcel.writeLong(updateTime)
        parcel.writeByte(if (isSystemApp) 1 else 0)
        parcel.writeInt(targetSdkVersion)
        parcel.writeInt(minSdkVersion)
        parcel.writeLong(apkSize)
        parcel.writeByte(if (supports32Bit) 1 else 0)
        parcel.writeByte(if (supports64Bit) 1 else 0)
        parcel.writeString(signatureInfo)
    }

    override fun describeContents(): Int {
        return 0
    }

    override fun toString(): String {
        return "AppInfo(appName='$appName', packageName='$packageName', versionName='$versionName')"
    }

    companion object CREATOR : Parcelable.Creator<AppInfo> {
        override fun createFromParcel(parcel: Parcel): AppInfo {
            return AppInfo(parcel)
        }

        override fun newArray(size: Int): Array<AppInfo?> {
            return arrayOfNulls(size)
        }
    }
}